package com.zh.oauth.controller;

import com.alibaba.fastjson.JSON;
import com.zh.common.entity.JSONResult;
import com.zh.common.util.OkHttpClientUtil;
import com.zh.common.constans.AuthConstant;
import com.zh.oauth.dao.OauthClientMapper;
import com.zh.common.entity.OauthClientDetails;
import com.zh.oauth.service.OauthClientService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import okhttp3.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import java.util.Objects;

/**
 * code is far away from bug with the animal protecting
 * ┏┓　　　┏┓
 * ┏┛┻━━━┛┻┓
 * ┃　　　　　　　┃
 * ┃　　　━　　　┃
 * ┃　┳┛　┗┳　┃
 * ┃　　　　　　　┃
 * ┃　　　┻　　　┃
 * ┃　　　　　　　┃
 * ┗━┓　　　┏━┛
 * 　　┃　　　┃神兽保佑
 * 　　┃　　　┃代码无BUG！
 * 　　┃　　　┗━━━┓
 * 　　┃　　　　　　　┣┓
 * 　　┃　　　　　　　┏┛
 * 　　┗┓┓┏━┳┓┏┛
 * 　　　┃┫┫　┃┫┫
 * 　　　┗┻┛　┗┻┛
 *
 * @Description: 客户端权限控制器
 * @Author: Li.HuiNeng
 * @Create in: 2019/8/20
 * @Version: 1.0.1
 */
@Api(value = "OauthClientController", tags = "客户端权限控制器")
@RestController
@RequestMapping("/authClient")
public class OauthClientController {

    @Autowired
    private OauthClientService oauthClientService;

    @Autowired
    private OauthClientMapper oauthClientMapper;

    @ApiOperation(value = "新增一条记录", tags = {"putOauthClientDetail"}, notes = "新增一条记录")
    @ResponseBody
    @PutMapping("/putOauthClientDetail")
    public JSONResult<Object> putOauthClientDetail(@RequestBody OauthClientDetails oauthClientDetails) {
        JSONResult<Object> jsonResult = new JSONResult<>();
        try {
        if (oauthClientDetails.insert()) {
            // 返回新增主键
//            System.out.println(oauthClientDetails.getClientId());
            jsonResult.setCode(200);
            jsonResult.setMessage("添加完成");
            jsonResult.setSuccess(true);
        } else {
            jsonResult.setMessage("添加失败,请检查参数是否填写完整!");
            jsonResult.setSuccess(false);
        }
    } catch (Exception e) {
        jsonResult.setCode(e.hashCode());
        jsonResult.setSuccess(false);
        jsonResult.setMessage(e.getLocalizedMessage());
        e.printStackTrace();
    }
        return jsonResult;
    }

    @PostMapping("/refreshToken")
    public JSONResult<Object> refreshToken(HttpServletRequest req, HttpServletResponse res){
        JSONResult<Object> jsonResult = new JSONResult<>();
        String token = req.getParameter("token");
        Map<String, String> bodyMap = new HashMap<>();
        bodyMap.put("grant_type", "refresh_token");
        bodyMap.put("refresh_token", token);
        Map<String, String> headMap = new HashMap<>();
        // 设置请求头
        headMap.put("Content-Type", "application/x-www-form-urlencoded");
        headMap.put("Authorization", "Basic Y2xpZW50OjEyMzQ1Ng==");
        Response response = OkHttpClientUtil.getInstance().postData(AuthConstant.URL_OAUTH_TOKEN, bodyMap, headMap);
        try {
            String jsonString = Objects.requireNonNull(response.body()).string();
            HashMap jsonMap = JSON.parseObject(jsonString, HashMap.class);
            jsonResult.setData(jsonMap);
            jsonResult.setCode(HttpStatus.OK.value());
            jsonResult.setSuccess(AuthConstant.boolT);
            return jsonResult;
        } catch (IOException e) {
            e.printStackTrace();
        }
        jsonResult.setSuccess(AuthConstant.boolF);
        jsonResult.setCode(HttpStatus.UNAUTHORIZED.value());
        jsonResult.setMessage("抱歉!您没有权限,请联系管理人员获取权限.");
        return  jsonResult;
    }

}
